package com.ch999.detect.View.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ch999.detect.AppBaseActivity;
import com.ch999.detect.R;
import com.ch999.detect.View.dialog.RestDetectDialog;
import com.ch999.detect.constant.EventType;
import com.ch999.detect.utils.CameraUtils;
import com.ch999.detect.utils.DeviceUtils;
import com.scorpio.mylib.ottoBusProvider.BusEvent;
import com.scorpio.mylib.ottoBusProvider.MyBusProvide;


public class DeviceInfoActivity extends AppBaseActivity {


    ImageView imgMenu;
    TextView tvMain;
    RelativeLayout llRootTitle;
    TextView tvPhoneName;
    TextView tvPhoneType;
    TextView tvPhoneMenony;
    TextView tvSystem;
    TextView tvIp;
    TextView tvPhoneImei;
    TextView tvIsRoot;
    TextView tvPhoneCPU;
    TextView tvPhoneCPUNum;
    TextView tvPhoneCPUHZ;


    LinearLayout activityDeviceInfo;
    TextView tvSystemMenony;

    private RestDetectDialog mRestDetectDialog;
    private TextView tvScreen;
    private TextView tvQian;
    private TextView tvBack;
    private TextView tvNfc;
    private TextView tvVision;
    private PackageManager mPackageManager;

    @Override
    public int getLayoutId() {
        return R.layout.activity_device_info_new;
    }

    @Override
    public void initView(Bundle savedInstanceState) {


    }


    @Override
    protected void onResume() {
        super.onResume();


        mPackageManager = getPackageManager();
        tvPhoneName.setText(DeviceUtils.getManufacturer());

        tvPhoneType.setText(DeviceUtils.getModel());

        mBaseHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                long totalMemorySize = DeviceUtils.getTotalMemorySize(mContext);
                tvPhoneMenony.setText(DeviceUtils.formatMemoryFileSize(totalMemorySize, true));

                long totalInternalMemorySize = DeviceUtils.getTotalInternalMemorySize();

                tvSystemMenony.setText(DeviceUtils.formatPhoneCardFileSize(totalInternalMemorySize, true));

                tvSystem.setText("Android " + DeviceUtils.getSystemVersion());

                tvIp.setText(DeviceUtils.getHostIP());

                tvPhoneImei.setText(DeviceUtils.getIMEI(mContext));
                tvIsRoot.setText(DeviceUtils.isDeviceRoot() ? "已Root" : "未Root");

                tvPhoneCPU.setText(DeviceUtils.getCpuName());
                tvPhoneCPUNum.setText(DeviceUtils.getNumCores() + "");
                tvPhoneCPUHZ.setText(DeviceUtils.formatFileSize(DeviceUtils.getMaxCPU(), false) + "HZ");
                tvScreen.setText(mScreenHeight + "x" + mScreenWidth);
                tvBack.setText(CameraUtils.getCameraPixels(CameraUtils.HasBackCamera()));
                tvQian.setText(CameraUtils.getCameraPixels(CameraUtils.HasFrontCamera()));
                boolean b = mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC);
                tvNfc.setText(b ? "支持" : "不支持");
                PackageInfo info = null;
                try {
                    info = mPackageManager.getPackageInfo(mContext.getPackageName(), 0);
                    String version = info.versionName;
                    tvVision.setText(version);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }, 1000);

    }

    private void startClear() {
     /*   Intent mIntent = new Intent();
        ComponentName comp = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
        mIntent.setComponent(comp);
        mIntent.setAction("android.intent.action.VIEW");
        startActivity(mIntent);*/

        startActivity(new Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS));
    }


    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.img_menu) {
            finish();

        } else if (i == R.id.img_back) {
            finish();

            //startClear();

        } else if (i == R.id.tv_clear) {
            startClear();

        } else if (i == R.id.btn_reset) {

            try {
                sendResetMessage();
            } catch (RuntimeException ex) {
            }



        }
    }

    /**
     * 创建重新检测的dialog
     */
    private void createRestDetectDialog() {
        if (mRestDetectDialog == null) {

            mRestDetectDialog = new RestDetectDialog(mContext, new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (view.getId() == R.id.btn_reset) {

                        mBaseHandler.sendEmptyMessageDelayed(0, 100);
                        mBaseHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                sendResetMessage();
                            }
                        }, 500);


                    }

                }
            });
        }
    }

    private void sendResetMessage() {
        dialog.show();

        BusEvent busEvent = new BusEvent();
        busEvent.setAction(EventType.MSG_ONCLICK_RESET.ordinal());
        MyBusProvide.getInstance().post(busEvent);

        mBaseHandler.sendEmptyMessageDelayed(0, 1000);
    }

    @Override
    public void findView() {
        imgMenu = (ImageView) findViewById(R.id.img_menu);
        tvMain = (TextView) findViewById(R.id.tv_main);
        llRootTitle = (RelativeLayout) findViewById(R.id.ll_root_title);
        tvPhoneName = (TextView) findViewById(R.id.tv_phone_name);
        tvPhoneType = (TextView) findViewById(R.id.tv_phone_type);
        tvPhoneMenony = (TextView) findViewById(R.id.tv_phone_menony);
        tvSystem = (TextView) findViewById(R.id.tv_system);
        tvIp = (TextView) findViewById(R.id.tv_ip);
        tvPhoneImei = (TextView) findViewById(R.id.tv_phone_imei);
        tvIsRoot = (TextView) findViewById(R.id.tv_is_root);
        tvPhoneCPU = (TextView) findViewById(R.id.tv_phone_cpu);
        tvPhoneCPUNum = (TextView) findViewById(R.id.tv_phone_cpu_num);
        tvPhoneCPUHZ = (TextView) findViewById(R.id.tv_phone_cpu_hz);

        activityDeviceInfo = (LinearLayout) findViewById(R.id.activity_device_info);
        tvSystemMenony = (TextView) findViewById(R.id.tv_system_menony);
        tvScreen = (TextView) findViewById(R.id.tv_screen);
        tvQian = (TextView) findViewById(R.id.tv_qian);
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvNfc = (TextView) findViewById(R.id.tv_nfc);
        tvVision = (TextView) findViewById(R.id.tv_vasion);

    }
}
